package com.dhl.iterator.college;

import com.dhl.iterator.Department;
import com.dhl.iterator.Iterator;

/**
 * @title: ComputerCollegeIterator
 * @Author danghailiang
 * @Date: 2022/8/20 16:15
 * @Version 1.0
 */
// 迭代器实现
public class ComputerCollegeIterator implements Iterator {

    private Department[] departments;
    /**
     * 数组指针
     */
    private Integer position = 0;

    public ComputerCollegeIterator(Department[] departments) {
        this.departments = departments;
    }

    @Override
    public boolean hasNext() {
        if (position >= departments.length || departments[position] == null) {
            return false;
        }
        return true;
    }

    @Override
    public Object next() {
        Department department = departments[position];
        position += 1;
        return department;
    }

    @Override
    public void remove(Object target) {
        departments[ Integer.parseInt(target.toString()) ] = null;
    }
}
